package com.dragon.common.spring.enhancers.advices;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Chris Chan
 * Create On 2025/8/5 下午7:20
 * Use for:
 * Explain:
 */
public class HttpRequestHttpInputMessage implements HttpInputMessage {
    private InputStream bodyInputStream;
    private HttpHeaders headers;

    public HttpRequestHttpInputMessage() {
    }

    public HttpRequestHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
        this.bodyInputStream = inputMessage.getBody();
        this.headers = inputMessage.getHeaders();
    }

    public static HttpRequestHttpInputMessage of(HttpInputMessage inputMessage) throws IOException {
        return new HttpRequestHttpInputMessage(inputMessage);
    }

    public static HttpRequestHttpInputMessage of(String bodyJson, HttpHeaders headers) {
        HttpRequestHttpInputMessage httpInputMessage = new HttpRequestHttpInputMessage();
        httpInputMessage.bodyInputStream = new ByteArrayInputStream(bodyJson.getBytes());
        httpInputMessage.headers = headers;
        return httpInputMessage;
    }

    @Override
    public InputStream getBody() throws IOException {
        return bodyInputStream;
    }

    @Override
    public HttpHeaders getHeaders() {
        return headers;
    }
}
